//
//  SearchVC.m
//  echo
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "SearchVC.h"
#import "NetworkingHandler.h"
#import "ModelForSearch.h"
#import "CellForMyExploreTableViewSectionThreeAndFour.h"
#import <DKNightVersion.h>
#import "CellForSearchVC.h"
#import "CellForSearchIsNull.h"
#import "MusicPlayerVC.h"

@interface SearchVC ()<UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITextField *textFieldForSearch;
@property (nonatomic, retain)UIView *backViewForGray;
@property (nonatomic, retain)UIView *backViewForWhite;
@property (nonatomic, retain)UIImageView *imageForSearch;
@property (nonatomic, retain)UIButton *buttonForCancel;
@property (nonatomic, retain)NSString *textFieldContent;
@property (nonatomic, retain)NSMutableArray *arrForData;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIView *backViewForTableView;
@end

@implementation SearchVC

-(void)dealloc{
    [_backViewForGray release];
    [_backViewForWhite release];
    [_imageForSearch release];
    [_buttonForCancel release];
    [_textFieldContent release];
    [_arrForData release];
    [_backViewForTableView release];
    self.textFieldForSearch.delegate = nil;
    [_textFieldForSearch release];
    self.tableView.delegate = nil;
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationBarHidden = YES;
    [self createViews];
}

- (void)handleData{
    self.textFieldContent = self.textFieldForSearch.text;
    NSString *str = @"http://echosystem.kibey.com/search/sound?page=1&with_channel_user=1&src=1&keyword=";
    NSString *url = [str stringByAppendingString:self.textFieldContent];
    
    [NetworkingHandler networkingGetWithUrl:url dicForValue:@{@"x-v":@"20160209",@"x-c":@"3",@"Cookie":@"PHPSESSID=tje4p58ah6fpmo7lhpf51oods6"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *arr = [dic objectForKey:@"data"];
        self.arrForData = [NSMutableArray array];
        for (NSDictionary *tempDic in arr) {
            ModelForSearch *model = [[ModelForSearch alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrForData addObject:model];
            [model release];
        }
        [self createTableView];
    }];
}

- (void)createViews{
    self.backViewForGray = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70 * HEIGHT)];
    [self.view addSubview:self.backViewForGray];
    [self.backViewForGray release];
    
    self.backViewForWhite = [[UIView alloc]initWithFrame:CGRectMake(10 * WIDTH, 28 * HEIGHT, self.view.frame.size.width - 75 * WIDTH, 35 * HEIGHT)];
    [self.backViewForGray addSubview:self.backViewForWhite];
    [self.backViewForWhite release];
    self.backViewForWhite.backgroundColor = [UIColor whiteColor];
    self.backViewForWhite.layer.cornerRadius = 10 * WIDTH;
    self.backViewForWhite.layer.masksToBounds = YES;
    self.textFieldForSearch = [[UITextField alloc]initWithFrame:CGRectMake(38 * WIDTH, 0, self.view.frame.size.width - 113 * WIDTH, 35 * HEIGHT)];
    [self.backViewForWhite addSubview:self.textFieldForSearch];
    [self.textFieldForSearch release];
    self.textFieldForSearch.backgroundColor = [UIColor whiteColor];
    [self.textFieldForSearch setPlaceholder:@"搜索"];
    self.textFieldForSearch.keyboardType = UIKeyboardTypeDefault;
    self.textFieldForSearch.returnKeyType = UIReturnKeySearch;
    self.textFieldForSearch.delegate = self;
    
    self.textFieldForSearch.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.imageForSearch = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-sousuo"]];
    [self.backViewForWhite addSubview:self.imageForSearch];
    [self.imageForSearch release];
    self.imageForSearch.frame = CGRectMake(3.5 * WIDTH, 3.5 * HEIGHT, 28 * WIDTH, 28 * WIDTH);
    
    self.buttonForCancel = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonForCancel.frame = CGRectMake(10 * WIDTH + self.view.frame.size.width - 75 * WIDTH + 10 * WIDTH, 28 * HEIGHT, 45 * WIDTH, 35 * WIDTH);
    [self.backViewForGray addSubview:self.buttonForCancel];
    self.buttonForCancel.tintColor = [UIColor greenColor];
    self.buttonForCancel.layer.cornerRadius = 5 * WIDTH;
    self.buttonForCancel.layer.masksToBounds = YES;
    [self.buttonForCancel setTitle:@"取消" forState:UIControlStateNormal];
    [self.buttonForCancel addTarget:self action:@selector(handleCancel) forControlEvents:UIControlEventTouchUpInside];
    
    self.backViewForTableView = [[UIView alloc]initWithFrame:CGRectMake(0, 70 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 70 * HEIGHT)];
    [self.view addSubview:self.backViewForTableView];
    [self.backViewForTableView release];
}

- (void)handleCancel{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

        [self.textFieldForSearch resignFirstResponder];
        [self handleData];

    
    return YES;
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 70 * HEIGHT)];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = NO;
    [self.tableView registerClass:[CellForSearchVC class] forCellReuseIdentifier:@"poolOfDataForSearch"];
    [self.tableView registerClass:[CellForSearchIsNull class] forCellReuseIdentifier:@"poolForSearchIsNull"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *viewForBackOfHeader =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHT)];
    viewForBackOfHeader.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    UILabel *labelForHeader = [[UILabel alloc]initWithFrame:CGRectMake(15 * WIDTH, 10 * HEIGHT, 100 * WIDTH, 25 * HEIGHT)];
    labelForHeader.text = @"   相关回声";
    labelForHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    [viewForBackOfHeader addSubview:labelForHeader];
    return viewForBackOfHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * HEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.arrForData.count == 0) {
        return 1;
    }else{
        return self.arrForData.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrForData.count == 0) {
        CellForSearchIsNull *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSearchIsNull"];
        return cell;
    }else{
        CellForSearchVC *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfDataForSearch"];
        cell.textFieldText = self.textFieldForSearch.text;
        ModelForSearch *model = [[ModelForSearch alloc]init];
        model = [self.arrForData objectAtIndex:indexPath.row];
        [cell passDataForSearch:model];
        //取消选中Cell灰色效果
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrForData.count == 0) {
        return 400;
    }else{
        return 82.2 * HEIGHT;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelForSearch *model = [self.arrForData objectAtIndex:indexPath.row];
    MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
    [vc passModelModelForSearchVC:model];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.backViewForGray.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 255.f green:27 / 255.f blue:47 / 255.f alpha:1]);
    
    self.buttonForCancel.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 255.f green:27 / 255.f blue:47 / 255.f alpha:1]);
    
    self.backViewForTableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 255.f green:27 / 255.f blue:47 / 255.f alpha:1]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
